local H = require "packages/activities/HuanhuaFunc"

-- 修改一名角色的灵力数
---@param player ServerPlayer
---@param num integer
local function addSiritualPower(player, num)
  if num > 0 then
    player.room:addPlayerMark(player, "@!spiritualPower", num)
  else
    player.room:removePlayerMark(player, "@!spiritualPower", -num)
  end
end

-- 获知一名角色的灵力数
---@param player Player
---@return integer
local function getSpiritualPower(player)
  return player:getMark("@!spiritualPower")
end

-- 获知一名角色的聚灵技能
---@param player Player
---@return string[]
local function getJulingSkills(player)
  return player:getTableMark("JulingSkills")
end

-- 设置一名角色的聚灵技能
---@param player ServerPlayer
---@param skill string
local function setJulingSkill(player, skill)
  local room = player.room
  local juling_skills = getJulingSkills(player)
  if #juling_skills == 3 then
    local tolose = room:askForChoice(player, juling_skills, "spiritualPower&", "#JulingLose", true) or juling_skills[1]
    table.removeOne(juling_skills, tolose)
    room:handleAddLoseSkills(player, "-"..tolose)
  end
  room:handleAddLoseSkills(player, skill)
  table.insert(juling_skills, skill)
  room:setPlayerMark(player, "JulingSkills", juling_skills)
  local translate = table.map(juling_skills, function(s) return Fk:translate(s) end)
  room:setPlayerMark(player, "@JulingSkills", "<font color='gold'>" .. table.concat(translate, " ") .. "</font>")
end

-- 聚灵！
---@param player ServerPlayer
local function doJuling(player)
  local room = player.room
  local choices, beta_choices = H.getSkills(room, 3), {}
  addSiritualPower(player, -2)
  if getSpiritualPower(player) > 0 then
    table.insert(choices, "HuanhuaRefresh")
  end
  local choice = room:askForChoice(player, choices, "spiritualPower&", "#Juling-choose", true)
  if not choice then choice = choices[1] end
  if choice == "HuanhuaRefresh" then
    addSiritualPower(player, -1)
    beta_choices = H.getSkills(room, 3)
    choice = room:askForChoice(player, beta_choices, "spiritualPower&", "#Juling-choose", true)
  end
  if not choice then choice = choices[1] end
  setJulingSkill(player, choice)
  for _, t in ipairs{choices, beta_choices} do
    H.returnToSkillPool(room, table.filter(t, function(s) return s ~= "HuanhuaRefresh" and s ~= choice end))
  end
end

-- 创建虚拟卡牌用于聚灵摸牌
local spiritual_power_card = fk.CreateBasicCard{
  name = "_spiritual_power_card",
  suit = Card.NoSuit,
  number = 0,
  target_num = 0,
  can_use = function(self, player)
    return getSpiritualPower(player) > 0 and player:usedSkillTimes("spiritualPower&", Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, use)
    local player = room:getPlayerById(use.from)
    local tolose = use.extra_data and use.extra_data.spin_data or 1
    if tolose > getSpiritualPower(player) then
      tolose = getSpiritualPower(player)
    end
    addSiritualPower(player, -tolose)
    player:drawCards(tolose, "spiritualPower&")
  end,
}

Fk:addCard(spiritual_power_card)

-- 创建聚灵技能
local spiritualPower = fk.CreateSkill{
  name = "spiritualPower&",
}

-- 回合开始时聚灵触发
spiritualPower:addEffect(fk.TurnStart, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    return target == player and getSpiritualPower(player) > 1
  end,
  on_refresh = function(self, event, target, player, data)
    doJuling(player)
  end,
})

-- 提供聚灵摸牌的交互
spiritualPower:addEffect(fk.AskForUseCard, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    return target == player and getSpiritualPower(player) > 0 and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local max_sp = getSpiritualPower(player)
    
    -- 使用自定义对话框来模拟旋钮选择
    local choice = room:askForChoice(player, {"UseSpiritualPower", "Cancel"}, "spiritualPower&", "#JulingDraw", true)
    
    if choice == "UseSpiritualPower" then
      -- 创建虚拟使用数据
      local use_data = {
        card = spiritual_power_card,
        from = player.id,
        tos = {},
        extra_data = {
          spin_data = math.min(max_sp, 2)  -- 默认使用2点灵力
        }
      }
      
      -- 执行聚灵摸牌
      spiritual_power_card.on_use(spiritual_power_card, room, use_data)
    end
  end,
})

Fk:loadTranslationTable{
  ["spiritualPower&"] = "聚灵",
  [":spiritualPower&"] = "回合开始时，你可以移除2枚「灵力」，从3个随机技能中选一个获得；"
  .."出牌阶段限一次，你可以移除任意枚「灵力」，摸等量的牌。",
  ["HuanhuaRefresh"] = "刷新选项",
  [":HuanhuaRefresh"] = "消耗1枚「灵力」，刷新选项！",
  ["#Juling-choose"] = "聚灵：选择以下一个技能获得",
  ["@JulingSkills"] = "",
  ["#JulingLose"] = "聚灵：技能数量超出限制，选择以下1个已有的技能失去",
  ["#JulingDraw"] = "聚灵：可以移除任意枚「灵力」，摸等量的牌",
  ["UseSpiritualPower"] = "使用聚灵摸牌",
  ["Cancel"] = "取消",
}

return spiritualPower